﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSharp.Libs.ExceptionHandling;
using System.Collections;
using CSharp.Libs.Koc.ItemClasses;

namespace CSharp.Libs.Koc
{
    public class SeedHolder : IDisposable
    {
        #region declarations
        private Kernel _KRNL = null;

        private Hashtable _htSeed = null;

        private Dictionary<string, CityItem> _dictCities = new Dictionary<string, CityItem>();
        private Dictionary<string, CityKnightsItem> _dictCityKnights = new Dictionary<string, CityKnightsItem>();
        private Dictionary<string, CityLeadersItem> _dictCityLeaders = new Dictionary<string, CityLeadersItem>();
        #endregion
        


        #region constructor / finalizer
        public SeedHolder(
            Kernel KRNL)
        {
            _KRNL = KRNL;
        }

        public SeedHolder(
            Kernel KRNL,
            Hashtable htSeed)
            : this(KRNL)
        {
            _htSeed = htSeed;
            _analyzeSeed();
        }


        public void Dispose()
        {
            _dictCities = null;
            _htSeed = null;
            _KRNL = null;
        }

        ~SeedHolder()
        {
        }
        #endregion
        
        
        
        #region event handling
        #endregion
        
        
        
        #region private functions
        /// <summary>_analyzeSeed</summary>
        /// <history>created  14.02.2012</history>
        private void _analyzeSeed()
        {
            //Cities
            _readCities();

            //Knights
            _readKnights();

            //Leaders
            _readLeaders();
        }

        /// <summary>_readCities</summary>
        /// <history>created  14.02.2012</history>
        private void _readCities()
        {
            try
            {
                //get City list of seed
                ArrayList aryCities = _htSeed[constants.KEY_SEED_CITIES] as ArrayList;
                if (aryCities != null && aryCities.Count > 0)
                {
                    //clear cities 
                    _dictCities.Clear();

                    //cycle through cities
                    foreach (ArrayList aryCityItem in aryCities)
                    {
                        //create city item and put to dictionary
                        CityItem CI = new CityItem(_KRNL, aryCityItem);
                        _dictCities.Add(CI.CityID.ToString(), CI);
                    }
                }
            }
            catch (Exception ex)
            {
                _KRNL.EC.AddException(ex, this.ToString(), _KRNL.EC.Tools.GetCurrentMethodName());
            }
        }

        /// <summary>_readKnights</summary>
        /// <history>created  15.02.2012</history>
        private void _readKnights()
        {
            try
            {
                //get cities knights collection
                Hashtable htCitiesKnights = _htSeed[constants.KEY_SEED_KNIGHTS] as Hashtable;
                if (htCitiesKnights != null)
                {
                    //Clear CityKnights Collection
                    _dictCityKnights.Clear();

                    foreach (DictionaryEntry deCityKnights in htCitiesKnights)
                    {
                        string strCityID = _KRNL.GetObjectValue(deCityKnights.Key, KernelTools.VariableTypes.String);
                        Hashtable htKnights = deCityKnights.Value as Hashtable;

                        CityKnightsItem CKI = new CityKnightsItem();
                        CKI.CityId = strCityID;
                        CKI.Knights.Clear();

                        if (htKnights != null)
                        {
                            foreach (DictionaryEntry deKnight in htKnights)
                            {
                                string _strKnightId = _KRNL.GetObjectValue(deKnight.Key, KernelTools.VariableTypes.String);
                                Hashtable htKnight = deKnight.Value as Hashtable;

                                if (htKnight != null)
                                {
                                    KnightItem KI = new KnightItem(_KRNL, htKnight);
                                    CKI.Knights.Add(_strKnightId, KI);
                                }
                            }
                        }

                        _dictCityKnights.Add(strCityID, CKI);
                    }
                }
            }
            catch (Exception ex)
            {
                _KRNL.EC.AddException(ex, this.ToString(), _KRNL.EC.Tools.GetCurrentMethodName());
            }
        }

        /// <summary>_readLeaders</summary>
        /// <history>created  17.02.2012</history>
        private void _readLeaders()
        {
            try
            {
                //get leaders hashtable of seed
                Hashtable htCitiesLeaders = _htSeed[constants.KEY_SEED_LEADERS] as Hashtable;
                if (htCitiesLeaders != null)
                {
                    //Clear dictionary
                    _dictCityLeaders.Clear();

                    //cycle through cities
                    foreach (DictionaryEntry deCityLeaders in htCitiesLeaders)
                    {
                        //get leaders of current city
                        Hashtable htCityLeaders = deCityLeaders.Value as Hashtable;
                        if (htCityLeaders != null)
                        {
                            CityLeadersItem CLI = new CityLeadersItem();
                            CLI.CityID = _KRNL.GetObjectValue(deCityLeaders.Key, KernelTools.VariableTypes.String);
                            CLI.Leaders = new Dictionary<KnightRoles, long>();
                            CLI.Leaders.Add(KnightRoles.Foreman, _KRNL.GetHashtableValue(htCityLeaders, constants.KEY_SEED_POLITICS_KNIGHT_ID, KernelTools.VariableTypes.Int64));
                            CLI.Leaders.Add(KnightRoles.Marshal, _KRNL.GetHashtableValue(htCityLeaders, constants.KEY_SEED_COMBAT_KNIGHT_ID, KernelTools.VariableTypes.Int64));
                            CLI.Leaders.Add(KnightRoles.Alchemystic, _KRNL.GetHashtableValue(htCityLeaders, constants.KEY_SEED_INTELLIGENCE_KNIGHT_ID, KernelTools.VariableTypes.Int64));
                            CLI.Leaders.Add(KnightRoles.Steward, _KRNL.GetHashtableValue(htCityLeaders, constants.KEY_SEED_RESOURCEFULNESS_KNIGHT_ID, KernelTools.VariableTypes.Int64));

                            _dictCityLeaders.Add(CLI.CityID, CLI);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _KRNL.EC.AddException(ex, this.ToString(), _KRNL.EC.Tools.GetCurrentMethodName());
            }
        }
        #endregion
        
        
        
        #region interface
        #region properties
        /// <summary>Cities</summary>
        /// <history>created  14.02.2012</history>
        public Dictionary<string, CityItem> Cities
        {
            get
            {
                return _dictCities;
            }
        }

        /// <summary>CityKnights</summary>
        /// <history>created  15.02.2012</history>
        public Dictionary<string, CityKnightsItem> CityKnights
        {
            get
            {
                return _dictCityKnights;
            }
        }

        /// <summary>CityLeaders</summary>
        /// <history>created  17.02.2012</history>
        public Dictionary<string, CityLeadersItem> CityLeaders
        {
            get
            {
                return _dictCityLeaders;
            }
        }
        #endregion

        #region methods
        /// <summary>AnalyzeSeed</summary>
        /// <history>created  14.02.2012</history>
        public void AnalyzeSeed()
        {
            _analyzeSeed();
        }
        #endregion
        #endregion
    }
}
